home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Devices / CD-ROM / How to Detect a CD / Not used in this example / IsDriverOpen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-18  |  1.4 KB  |  72 lines  |  [TEXT/MPS ]

  1. #include <Memory.h>
  2. #include <Devices.h>
  3. #include <LowMem.h>
  4. #include "IsDriverOpen.h"
  5.  
  6. // Return driver ref if driver is open
  7.  
  8. short IsDriverOpen(StringPtr driverName)
  9. {
  10.     short        dref;
  11.     DCtlHandle    dceHndl;
  12.     
  13.     dref = 0;
  14.     
  15.     dceHndl = FindTheDriver(driverName);
  16.     if (dceHndl != NULL) {
  17.         
  18.         if ((*dceHndl)->dCtlFlags & dOpenedMask)        //    if open (bit 5)
  19.             dref = (*dceHndl)->dCtlRefNum;
  20.     }
  21.     
  22.     return dref;
  23. }
  24.  
  25.  
  26. //    FindTheDriver
  27. //    Return driver dctlhandle if it exists.
  28.  
  29. DCtlHandle FindTheDriver(StringPtr driverName)
  30. {
  31.     DCtlHandle    EntryHand;
  32.     short        count;
  33.     DCtlHandle    *utable;
  34.     
  35.     EntryHand = NULL;
  36.  
  37. //    number of entries in unit table.  LMGetUnitTableEntryCount isn't defined
  38. //  for PowerPC, but this does the equivalent.    
  39.     count = GetPtrSize(LMGetUTableBase()) / sizeof(DCtlHandle);    
  40.     utable = (DCtlEntry ***) LMGetUTableBase();
  41.     
  42.     while (--count >= 0) {
  43.         DCtlHandle    entry;
  44.         
  45.         entry = *utable++;
  46.         if (entry != NULL) {
  47.             StringPtr    namePtr;
  48.             
  49.         //    see if ram based (test bit 6)
  50.             
  51.             if ((*entry)->dCtlFlags & dRAMBasedMask) {
  52.                 
  53.             //    in ram, so we have a handle
  54.                 namePtr = (StringPtr) (*(Handle)((*entry)->dCtlDriver)) + 18;
  55.                 
  56.             } else {
  57.                 
  58.             //    in rom, so we have a pointer
  59.                 namePtr = (StringPtr) ((*entry)->dCtlDriver) + 18;
  60.             }
  61.             
  62.             if (RelString(driverName, namePtr, FALSE, TRUE) == 0) {
  63.                 
  64.                 EntryHand = entry;
  65.                 break;
  66.             }
  67.         }
  68.     }
  69.     
  70.     return (EntryHand);
  71. }
  72.